/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pl.makimo.ksr;

import java.text.DecimalFormat;
import java.util.HashMap;

import pl.makimo.data.WeatherRecord;
import pl.makimo.fuzzy.linguistics.DiscreteLinguisticVariable;
import pl.makimo.fuzzy.linguistics.QualityMeasures;
import pl.makimo.fuzzy.linguistics.Quantifier;
import pl.makimo.fuzzy.linguistics.Summary;
import pl.makimo.fuzzy.linguistics.SummaryFactory;
import pl.makimo.fuzzy.norms.MinNorm;
import pl.makimo.fuzzy.norms.Type2Norm;
import pl.makimo.fuzzy.sets.CompoundFuzzySet;
import pl.makimo.fuzzy.sets.ContinuousFuzzySet;
import pl.makimo.fuzzy.sets.CrispSet;
import pl.makimo.fuzzy.sets.DiscreteFuzzySet;
import pl.makimo.fuzzy.sets.Type2CompoundFuzzySet;
import pl.makimo.fuzzy.sets.Type2DiscreteFuzzySet;
import pl.makimo.fuzzy.sets.memberships.DiscreteMembershipFunction;
import pl.makimo.fuzzy.sets.memberships.TrapezoidMembershipFunction;
import pl.makimo.fuzzy.sets.memberships.TriangularMembershipFunction;
import pl.makimo.fuzzy.sets.memberships.Type2TriangularGaussianMembershipFunction;
import pl.makimo.fuzzy.sets.memberships.Type2TriangularIntervalMembershipFunction;

/**
 *
 * @author Mateusz Papiernik
 */
public class Main {

	public static void testType1() {
    	CrispSet<WeatherRecord> list = WeatherRecord.getFromFile("C:\\repos\\uczelnia\\ksr-gobbledygook\\weatherdb.csv");
    	
        /* Temperature */
        
    	DiscreteLinguisticVariable<WeatherRecord> var_s = new DiscreteLinguisticVariable<WeatherRecord>("temperature", list);
    	var_s.add("low", new DiscreteFuzzySet<WeatherRecord>(new TrapezoidMembershipFunction(0.0, 5.0, 5.0, 10.0)));
    	var_s.add("medium", new DiscreteFuzzySet<WeatherRecord>(new TrapezoidMembershipFunction(10.0, 15.0, 15.0, 20.0)));
    	var_s.add("high", new DiscreteFuzzySet<WeatherRecord>(new TrapezoidMembershipFunction(20.0, 25.0, 25.0, 30.0)));
    	
    	/* Humidity */
    	
    	DiscreteLinguisticVariable<WeatherRecord> var_s2 = new DiscreteLinguisticVariable<WeatherRecord>("humidity", list);
    	var_s2.add("low", new DiscreteFuzzySet<WeatherRecord>(new TrapezoidMembershipFunction(0.0, 20.0, 20.0, 50.0)));
    	var_s2.add("medium", new DiscreteFuzzySet<WeatherRecord>(new TrapezoidMembershipFunction(40.0, 60.0, 60.0, 80.0)));
    	var_s2.add("high", new DiscreteFuzzySet<WeatherRecord>(new TrapezoidMembershipFunction(70.0, 80.0, 80.0, 100.0)));
    	
    	/* Wind */
    	
    	DiscreteLinguisticVariable<WeatherRecord> var_s3 = new DiscreteLinguisticVariable<WeatherRecord>("wind direction", list);
    	var_s3.add("south", new DiscreteFuzzySet<WeatherRecord>(new DiscreteMembershipFunction(new HashMap<Object, Object>() {{ put("South", 0.1); }})));
    	
    	/* Summary factory */
    	
    	SummaryFactory.clear();
    	SummaryFactory.addVariable(var_s);
    	SummaryFactory.addVariable(var_s2);
    	SummaryFactory.addVariable(var_s3);
    	
    	SummaryFactory.addQuantifier(Quantifier.make(new ContinuousFuzzySet(new TrapezoidMembershipFunction(0.0, 0.0, 0.0, 0.5)), "less than half", list, true));
    	
    	SummaryFactory.summarizers.add("humidity");
    	SummaryFactory.summarizers.add("temperature");
    	
    	SummaryFactory.qualifiers.add("wind direction");
    	
    	for (Summary<WeatherRecord> summary : SummaryFactory.getSummaries()) {
        	System.out.print(summary); System.out.print(";");
        	System.out.print(QualityMeasures.T1(summary)); System.out.print(";");
        	System.out.print(QualityMeasures.T2(summary)); System.out.print(";");
        	System.out.print(QualityMeasures.T3(summary)); System.out.print(";");
        	System.out.print(QualityMeasures.T4(summary)); System.out.print(";");
        	System.out.print(QualityMeasures.T5(summary)); System.out.print(";");
        	System.out.print(QualityMeasures.T6(summary)); System.out.print(";");
        	System.out.print(QualityMeasures.T7(summary)); System.out.print(";");
        	System.out.print(QualityMeasures.T8(summary)); System.out.print(";");
        	System.out.print(QualityMeasures.T9(summary)); System.out.print(";");
        	System.out.print(QualityMeasures.T10(summary)); System.out.print(";");
        	System.out.print(QualityMeasures.T11(summary)); System.out.print(";");
        	System.out.println();
    	}
	}
	
	public static void testType1Zad() {
    	CrispSet<WeatherRecord> list = WeatherRecord.getFromFile("C:\\repos\\uczelnia\\ksr-gobbledygook\\weatherdb.csv");
    	
        /* Temperature */
        
    	DiscreteLinguisticVariable<WeatherRecord> var_s = new DiscreteLinguisticVariable<WeatherRecord>("temperature", list);
    	//var_s.add("low", new DiscreteFuzzySet<WeatherRecord>(new TrapezoidMembershipFunction(0.0, 5.0, 5.0, 10.0)));
    	var_s.add("average", new Type2DiscreteFuzzySet<WeatherRecord>(new Type2TriangularGaussianMembershipFunction(10.0, 15.0, 20.0, 0.2)));
    	//var_s.add("high", new DiscreteFuzzySet<WeatherRecord>(new TrapezoidMembershipFunction(20.0, 25.0, 25.0, 30.0)));
    	
    	/* Humidity */
    	
    	DiscreteLinguisticVariable<WeatherRecord> var_s2 = new DiscreteLinguisticVariable<WeatherRecord>("humidity", list);
    	//var_s2.add("low", new DiscreteFuzzySet<WeatherRecord>(new TrapezoidMembershipFunction(0.0, 20.0, 20.0, 50.0)));
    	var_s2.add("average", new Type2DiscreteFuzzySet<WeatherRecord>(new Type2TriangularGaussianMembershipFunction(40.0, 60.0, 80.0, 0.3)));
    	//var_s2.add("high", new DiscreteFuzzySet<WeatherRecord>(new TrapezoidMembershipFunction(70.0, 80.0, 80.0, 100.0)));
    	
    	/* Wind */
    	
    	DiscreteLinguisticVariable<WeatherRecord> var_s3 = new DiscreteLinguisticVariable<WeatherRecord>("wind direction", list);
    	var_s3.add("south", new DiscreteFuzzySet<WeatherRecord>(new DiscreteMembershipFunction(new HashMap<Object, Object>() {{ put("South", 0.1); }})));
    	
    	/* Summary factory */
    	
    	SummaryFactory.clear();
    	SummaryFactory.addVariable(var_s);
    	SummaryFactory.addVariable(var_s2);
    	SummaryFactory.addVariable(var_s3);
    	
    	SummaryFactory.addQuantifier(Quantifier.make(new ContinuousFuzzySet(new TriangularMembershipFunction(0.0, 0.2, 0.4)), "minority", list, true));
    	SummaryFactory.addQuantifier(Quantifier.make(new ContinuousFuzzySet(new TriangularMembershipFunction(0.3, 0.5, 0.7)), "about half", list, true));
    	SummaryFactory.addQuantifier(Quantifier.make(new ContinuousFuzzySet(new TriangularMembershipFunction(0.6, 0.8, 1.0)), "majority", list, true));
    	
    	SummaryFactory.qualifiers.add("humidity");
    	SummaryFactory.summarizers.add("temperature");
    	
    	// SummaryFactory.qualifiers.add("wind direction");
    	
    	for (Summary<WeatherRecord> summary : SummaryFactory.getSummaries()) {
    		 System.out.print("\\item "); System.out.println(summary);
    	}
    	
    	DecimalFormat df = new DecimalFormat("#.##");
    	
    	int i = 0;
    	
    	for (Summary<WeatherRecord> summary : SummaryFactory.getSummaries()) {
    		System.out.print(++i); System.out.print(" & ");
         	System.out.print(df.format(QualityMeasures.T1(summary))); System.out.print(" & ");
         	System.out.print(df.format(QualityMeasures.T2(summary))); System.out.print(" & ");
         	System.out.print(df.format(QualityMeasures.T3(summary))); System.out.print(" & ");
         	System.out.print(df.format(QualityMeasures.T4(summary))); System.out.print(" & ");
         	System.out.print(df.format(QualityMeasures.T5(summary))); System.out.print(" & ");
         	System.out.print(df.format(QualityMeasures.T6(summary))); System.out.print(" & ");
         	System.out.print(df.format(QualityMeasures.T7(summary))); System.out.print(" & ");
         	System.out.print(df.format(QualityMeasures.T8(summary))); System.out.print(" & ");
         	System.out.print(df.format(QualityMeasures.T9(summary))); System.out.print(" & ");
         	System.out.print(df.format(QualityMeasures.T10(summary))); System.out.print(" & ");
         	System.out.print(df.format(QualityMeasures.T11(summary))); System.out.print("\\\\");
         	System.out.println();   
    	}   
	}
	
	
	public static void testType2() {
    	CrispSet<WeatherRecord> list = WeatherRecord.getFromFile("C:\\repos\\uczelnia\\ksr-gobbledygook\\weatherdb.csv");
    	
        /* Temperature */
        
    	DiscreteLinguisticVariable<WeatherRecord> var_s = new DiscreteLinguisticVariable<WeatherRecord>("temperature", list);
    	var_s.add("low", Type2DiscreteFuzzySet.fromType1(new DiscreteFuzzySet<WeatherRecord>(new TrapezoidMembershipFunction(0.0, 5.0, 5.0, 10.0), list, "temperature")));
    	var_s.add("medium", Type2DiscreteFuzzySet.fromType1(new DiscreteFuzzySet<WeatherRecord>(new TrapezoidMembershipFunction(10.0, 15.0, 15.0, 20.0), list, "temperature")));
    	var_s.add("high", Type2DiscreteFuzzySet.fromType1(new DiscreteFuzzySet<WeatherRecord>(new TrapezoidMembershipFunction(20.0, 25.0, 25.0, 30.0), list, "temperature")));
    	
    	/* Humidity */
    	
    	DiscreteLinguisticVariable<WeatherRecord> var_s2 = new DiscreteLinguisticVariable<WeatherRecord>("humidity", list);
    	var_s2.add("low", Type2DiscreteFuzzySet.fromType1(new DiscreteFuzzySet<WeatherRecord>(new TrapezoidMembershipFunction(0.0, 20.0, 20.0, 50.0), list, "humidity")));
    	var_s2.add("medium", Type2DiscreteFuzzySet.fromType1(new DiscreteFuzzySet<WeatherRecord>(new TrapezoidMembershipFunction(40.0, 60.0, 60.0, 80.0), list, "humidity")));
    	var_s2.add("high", Type2DiscreteFuzzySet.fromType1(new DiscreteFuzzySet<WeatherRecord>(new TrapezoidMembershipFunction(70.0, 80.0, 80.0, 100.0), list, "humidity")));
    	
    	/* Wind */
    	
    	DiscreteLinguisticVariable<WeatherRecord> var_s3 = new DiscreteLinguisticVariable<WeatherRecord>("wind direction", list);
    	var_s3.add("south", Type2DiscreteFuzzySet.fromType1(new DiscreteFuzzySet<WeatherRecord>(new DiscreteMembershipFunction(new HashMap<Object, Object>() {{ put("South", 0.1); }}), list, "wind direction")));
    	
    	/* Summary factory */
    	
    	SummaryFactory.clear();
    	SummaryFactory.addVariable(var_s);
    	SummaryFactory.addVariable(var_s2);
    	SummaryFactory.addVariable(var_s3);
    	
    	SummaryFactory.addQuantifier(Quantifier.make(new ContinuousFuzzySet(new TrapezoidMembershipFunction(0.0, 0.0, 0.0, 0.5)), "less than half", list, true));
    	
    	SummaryFactory.summarizers.add("humidity");
    	SummaryFactory.summarizers.add("temperature");
    	
    	SummaryFactory.qualifiers.add("wind direction");    	
    	
    	for (Summary<WeatherRecord> summary : SummaryFactory.getSummaries()) {
        	System.out.print(summary); System.out.print(";");
        	System.out.print(QualityMeasures.T1(summary)); System.out.print(";");
        	System.out.print(QualityMeasures.T2(summary)); System.out.print(";");
        	System.out.print(QualityMeasures.T3(summary)); System.out.print(";");
        	System.out.print(QualityMeasures.T4(summary)); System.out.print(";");
        	System.out.print(QualityMeasures.T5(summary)); System.out.print(";");
        	System.out.print(QualityMeasures.T6(summary)); System.out.print(";");
        	System.out.print(QualityMeasures.T7(summary)); System.out.print(";");
        	System.out.print(QualityMeasures.T8(summary)); System.out.print(";");
        	System.out.print(QualityMeasures.T9(summary)); System.out.print(";");
        	System.out.print(QualityMeasures.T10(summary)); System.out.print(";");
        	System.out.print(QualityMeasures.T11(summary)); System.out.print(";");
        	System.out.println();
    	}
	}	
	
	public static void testTypeMixed() {
    	CrispSet<WeatherRecord> list = WeatherRecord.getFromFile("C:\\repos\\uczelnia\\ksr-gobbledygook\\weatherdb.csv");
    	
        /* Temperature */
        
    	DiscreteLinguisticVariable<WeatherRecord> var_s = new DiscreteLinguisticVariable<WeatherRecord>("temperature", list);
    	var_s.add("low", new DiscreteFuzzySet<WeatherRecord>(new TrapezoidMembershipFunction(0.0, 5.0, 5.0, 10.0)));
    	var_s.add("medium", new DiscreteFuzzySet<WeatherRecord>(new TrapezoidMembershipFunction(10.0, 15.0, 15.0, 20.0)));
    	var_s.add("high", new DiscreteFuzzySet<WeatherRecord>(new TrapezoidMembershipFunction(20.0, 25.0, 25.0, 30.0)));
    	
    	/* Humidity */
    	
    	DiscreteLinguisticVariable<WeatherRecord> var_s2 = new DiscreteLinguisticVariable<WeatherRecord>("humidity", list);
    	var_s2.add("low", Type2DiscreteFuzzySet.fromType1(new DiscreteFuzzySet<WeatherRecord>(new TrapezoidMembershipFunction(0.0, 20.0, 20.0, 50.0), list, "humidity")));
    	var_s2.add("medium", Type2DiscreteFuzzySet.fromType1(new DiscreteFuzzySet<WeatherRecord>(new TrapezoidMembershipFunction(40.0, 60.0, 60.0, 80.0), list, "humidity")));
    	var_s2.add("high", Type2DiscreteFuzzySet.fromType1(new DiscreteFuzzySet<WeatherRecord>(new TrapezoidMembershipFunction(70.0, 80.0, 80.0, 100.0), list, "humidity")));
    	
    	/* Wind */
    	
    	DiscreteLinguisticVariable<WeatherRecord> var_s3 = new DiscreteLinguisticVariable<WeatherRecord>("wind direction", list);
    	var_s3.add("south", Type2DiscreteFuzzySet.fromType1(new DiscreteFuzzySet<WeatherRecord>(new DiscreteMembershipFunction(new HashMap<Object, Object>() {{ put("South", 0.1); }}), list, "wind direction")));
    	
    	/* Summary factory */
    	
    	SummaryFactory.clear();
    	SummaryFactory.addVariable(var_s);
    	SummaryFactory.addVariable(var_s2);
    	SummaryFactory.addVariable(var_s3);
    	
    	SummaryFactory.addQuantifier(Quantifier.make(new ContinuousFuzzySet(new TrapezoidMembershipFunction(0.0, 0.0, 0.0, 0.5)), "less than half", list, true));
    	
    	SummaryFactory.summarizers.add("humidity");
    	SummaryFactory.summarizers.add("temperature");
    	
    	SummaryFactory.qualifiers.add("wind direction");    	
    	
    	for (Summary<WeatherRecord> summary : SummaryFactory.getSummaries()) {
        	System.out.print(summary); System.out.print(";");
        	System.out.print(QualityMeasures.T1(summary)); System.out.print(";");
        	System.out.print(QualityMeasures.T2(summary)); System.out.print(";");
        	System.out.print(QualityMeasures.T3(summary)); System.out.print(";");
        	System.out.print(QualityMeasures.T4(summary)); System.out.print(";");
        	System.out.print(QualityMeasures.T5(summary)); System.out.print(";");
        	System.out.print(QualityMeasures.T6(summary)); System.out.print(";");
        	System.out.print(QualityMeasures.T7(summary)); System.out.print(";");
        	System.out.print(QualityMeasures.T8(summary)); System.out.print(";");
        	System.out.print(QualityMeasures.T9(summary)); System.out.print(";");
        	System.out.print(QualityMeasures.T10(summary)); System.out.print(";");
        	System.out.print(QualityMeasures.T11(summary)); System.out.print(";");
        	System.out.println();
    	}
	}		
	
	public static void syntheticTest() {
    	CrispSet<WeatherRecord> list = WeatherRecord.getFromFile("C:\\repos\\uczelnia\\ksr-gobbledygook\\weatherdb.csv");
    	
    	DiscreteFuzzySet<WeatherRecord> type1_1 = new DiscreteFuzzySet<WeatherRecord>(new TrapezoidMembershipFunction(0.0, 5.0, 5.0, 10.0));
    	type1_1.elements = list;
    	type1_1.column = "temperature";

    	DiscreteFuzzySet<WeatherRecord> type1_2 = new DiscreteFuzzySet<WeatherRecord>(new TrapezoidMembershipFunction(0.0, 50, 50, 100));
    	type1_2.elements = list;
    	type1_2.column = "humidity";
    	
    	CompoundFuzzySet<WeatherRecord> type1_c = new CompoundFuzzySet<WeatherRecord>(type1_1, type1_2, new MinNorm());
    	
    	System.out.println("Type 1\n\n");
    	
    	System.out.println(type1_1);
    	System.out.println(type1_1.cardinality());
    	System.out.println(type1_1.support());
    	System.out.println(type1_1.support().cardinality());

    	System.out.println(type1_2);
    	System.out.println(type1_2.cardinality());
    	System.out.println(type1_2.support());
    	System.out.println(type1_2.support().cardinality());
    	
    	System.out.println(type1_c);
    	System.out.println(type1_c.cardinality());
    	System.out.println(type1_c.support());
    	System.out.println(type1_c.support().cardinality());    	
    	
    	/**/
    	
    	Type2DiscreteFuzzySet<WeatherRecord> type2_1 = Type2DiscreteFuzzySet.fromType1(type1_1);
    	Type2DiscreteFuzzySet<WeatherRecord> type2_2 = Type2DiscreteFuzzySet.fromType1(type1_2);
    	
    	Type2CompoundFuzzySet<WeatherRecord> type2_c = new Type2CompoundFuzzySet<WeatherRecord>(type2_1, type2_2, new Type2Norm());
    	
    	System.out.println("\n\nType 2\n\n");
    	
    	System.out.println(type2_1);
    	System.out.println(type2_1.cardinality());
    	System.out.println(type2_1.support());
    	System.out.println(type2_1.support().cardinality());
    	
    	System.out.println(type2_2);
    	System.out.println(type2_2.cardinality());
    	System.out.println(type2_2.support());
    	System.out.println(type2_2.support().cardinality());
    	
    	System.out.println(type2_c);
    	System.out.println(type2_c.cardinality());
    	System.out.println(type2_c.support());
    	System.out.println(type2_c.support().cardinality());		
	}

	public static void syntheticTest2() {
    	CrispSet<WeatherRecord> list = WeatherRecord.getFromFile("C:\\repos\\uczelnia\\ksr-gobbledygook\\weatherdb.csv");
    	
    	Type2DiscreteFuzzySet<WeatherRecord> type1_1 = new Type2DiscreteFuzzySet<WeatherRecord>(new Type2TriangularIntervalMembershipFunction(0.0, 5.0, 5.0, 0.4));
    	type1_1.elements = list;
    	type1_1.column = "temperature";
    	
    	System.out.println("Type 1\n\n");
    	
    	System.out.println(type1_1);
    	System.out.println(type1_1.cardinality());
    	System.out.println(type1_1.support());
    	System.out.println(type1_1.support().cardinality());
	}
	
	
    /**
     * @param args the command line arguments
     * @throws Exception 
     */
    public static void main(String[] args) throws Exception {
    	/*
    	System.out.println("\n\nType 1\n\n");
    	testType1();
    	System.out.println("\n\nType 2\n\n");
    	testType2();
    	System.out.println("\n\nType mixed\n\n");
    	testTypeMixed();    	
    	*/
    	
    	testType1Zad();
    	
    	//syntheticTest2();
    }
}
